//
//  UIView+Tap.m
//  chequanbao_ios
//
//  Created by pro on 17/5/18.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "UIView+Tap.h"

#import <objc/runtime.h>

static const void *kActionBlockKey = "kActionBlockKey";

@implementation UIView (Tap)

- (void)addTapAction:(void (^)(UITapGestureRecognizer *tap))action {
    
    objc_setAssociatedObject(self, kActionBlockKey, action, OBJC_ASSOCIATION_COPY);

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self addGestureRecognizer:tap];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    void (^action)(UITapGestureRecognizer *tap, __weak id wself) = objc_getAssociatedObject(self, kActionBlockKey);
    if (action) {
        action(tap, self);
    }
}

@end
